Google Pay Launcher
A drop-in class that presents a Google Pay sheet to collect a customer's payment details. When the launcher finishes, a Result will be returned via ResultCallback.onResult.
Use of this class will typically look like this:
Create an instance of this class
Create a button for Google Pay (see GooglePayButton) and hook it up to a click listener
Optionally use the readyCallback property to set visibility or enabled states
In the click handler, do the following:
Ensure Google Pay is ready (see the readyCallback property (or use the isReady property)
Call present to present the Google Pay sheet and begin the payment process
Use the data passed to ResultCallback.onResult to retrieve payment details and process the payment
Important:
The resultCallback property must be set before calling present
Example Implementation
class PaymentActivity : ComponentActivity(), ReadyCallback, ResultCallback {
lateinit var button: GooglePayButton
lateinit var launcher: IGooglePayLauncher
val gatewayJson = JSONObject()
.put("gateway", "freedompay")
.put("gatewayMerchantId", "MyMerchantId")
.toString()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.my_payment_view)
button = findViewById<GooglePayButton>(R.id.my_google_pay_button)
button.isEnabled = false
button.setOnClickListener {
if (!launcher.isReady) {
return@setOnClickListener
}
launcher.present(100)
}
launcher = GooglePayLauncher(
activity = this,
readyCallback = this,
resultCallback = this,
config = Configuration(
environment = SdkEnvironment.Test,
gatewayParametersJson = gatewayJson,
companyName = "Test Company"
)
)
}
// Update the button state when Google Pay readiness changes
override fun onReady(isReady: Boolean) {
button.isEnabled = isReady
}
// Handle the result of the Google Pay sheet
override fun onResult(result: Result) {
when (result) {
is Result.Completed -> {
// Submit the payment data for payment processing (generally to Olo's ordering API)
// This can be retrieved from result.paymentData
}
is Result.Canceled -> {
// The user canceled the payment
}
is Result.Failed -> {
// An error occurred while attempting to get payment details
// Details about the error can be retrieved from result.error
}
}
}
}Constructors
Constructor for using this class from a ComponentActivity.
Constructor for using this class from a Fragment.
Properties
Functions
Presents the Google Pay sheet. Use resultCallback to listen for and handle the result of showing the Google Pay sheet to the user.